package com.example.demo.service.impl;

import com.example.demo.dao.CustomerRepository;
import com.example.demo.entity.Customer;
import com.example.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.transaction.annotation.Propagation.*;
import static org.springframework.transaction.annotation.Propagation.NEVER;

/**
 * @auther yaoyouwei
 * @create 2020-03-25 22:08
 */
@Service("customerServiceBJpaImpl")
public class CustomerServiceBJpaImpl implements CustomerService {
    @Autowired
    CustomerRepository customerRepository;
    @Override
    public void addWithoutTransactional(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    public void addWithoutTransactionalExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }
    @Override
    @Transactional(propagation = REQUIRED,rollbackFor = Exception.class)
    public void addRequired(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = REQUIRES_NEW,rollbackFor = Exception.class)
    public void addRequiredNew(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = NESTED,rollbackFor = Exception.class)
    public void addNested(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = SUPPORTS,rollbackFor = Exception.class)
    public void addSupports(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = NOT_SUPPORTED,rollbackFor = Exception.class)
    public void addNotSupported(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = MANDATORY,rollbackFor = Exception.class)
    public void addMandatory(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = NEVER,rollbackFor = Exception.class)
    public void addNever(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    @Transactional(propagation = REQUIRED,rollbackFor = Exception.class)
    public void addRequiredExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = REQUIRES_NEW,rollbackFor = Exception.class)
    public void addRequiredNewExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = NESTED,rollbackFor = Exception.class)
    public void addNestedExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = SUPPORTS,rollbackFor = Exception.class)
    public void addSupportsExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = NOT_SUPPORTED,rollbackFor = Exception.class)
    public void addNotSupportedExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = MANDATORY,rollbackFor = Exception.class)
    public void addMandatoryExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    @Transactional(propagation = NEVER,rollbackFor = Exception.class)
    public void addNeverExc(Customer customer) {
        customerRepository.save(customer);
        throwExc();
    }

    @Override
    public void throwExc() {
        int i = 1/0;
    }
}
